home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / orca < prev    next >
Encoding:
Text File  |  2007-04-10  |  7.3 KB  |  261 lines

  1. #!/bin/bash
  2. #
  3. # Orca
  4. #
  5. # Copyright 2006-2007 Sun Microsystems Inc.
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Library General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public
  18. # License along with this library; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. # Boston, MA 02111-1307, USA.
  21.  
  22. # This script performs some clean up and will run Orca.  It will also
  23. # rerun Orca if it detects that Orca died an unnatural death.
  24.  
  25. # __id__        = "$Id: orca.in,v 1.22 2006/12/08 16:21:25 wwalker Exp $"
  26. # __version__   = "$Revision: 1.22 $"
  27. # __date__      = "$Date: 2006/12/08 16:21:25 $"
  28. # __copyright__ = "Copyright (c) 2005-2006 Sun Microsystems Inc."
  29. # __license__   = "LGPL"
  30.  
  31. # Set the user's $PATH for this script.
  32. #
  33. export PATH="/usr/ccs/bin:/usr/bin:/usr/sbin:/bin:/usr/sfw/bin:/usr/openwin/bin:/usr/X11R6/bin"
  34.  
  35. # If you set RUNONCE to "true", then this will just run Orca once and quit.
  36. #
  37. RUNONCE="false"
  38.  
  39. # A value of 1 means to run Orca.  A value of 0 means quit.
  40. #
  41. RUN=1
  42.  
  43. # Initially there will be no watchdog process id.
  44. #
  45. watchdog_pid=0
  46.  
  47. # Save the arguments away.
  48. #
  49. ARGS="$*"
  50.  
  51. # The watchdog will periodically ping Orca to see if it is responding.
  52. # If orca isn't responding, the watchdog will kill the Orca process.
  53. # The watchdog logic requires 'wget', so we won't do it if we can't
  54. # find wget in the path.  Note also that you can force WATCHDOG=0 if you
  55. # do not want a background process that periodically pings Orca to see
  56. # if it is responding.
  57. #
  58. IFS=:
  59. WGETCMD=
  60. WATCHDOG=0
  61. for dir in $PATH:/usr/sfw/bin:/usr/local/bin; do
  62.     test -x "$dir/wget" && {
  63.         WGETCMD="$dir/wget"
  64.         WATCHDOG=1
  65.         break
  66.     }
  67. done
  68.  
  69. # Cleans up any orca-related processes that might be running,
  70. # restricting it to those processes owned by the user. These include
  71. # orca itself, any gnome-speech synthesis drivers, and festival
  72. # processes running in server mode.
  73. #
  74. cleanup()
  75. {
  76.     USERID=`id | cut -f2 -d= | cut -f1 -d\(`
  77.     PIDS=`ps -eo pid,ruid,args | grep $USERID | egrep "orca[.]orca|synthesis-driver|festival [-][-]server" | grep -v grep | awk '{ print $1 }'`
  78.  
  79.     IFS='
  80.     '
  81.     PIDS=`echo $PIDS`
  82.  
  83.     if [ "x$PIDS" != "x" ]
  84.     then
  85.         kill -9 $PIDS > /dev/null 2>&1
  86.     fi
  87. }
  88.  
  89. # Runs orca.
  90. #
  91. runOrca()
  92. {
  93.     if [ -z "$1" ]
  94.     then
  95.         cleanup
  96.     fi
  97.     exec_prefix=/usr
  98.     PYTHONPATH=${exec_prefix}/lib/python2.5/site-packages
  99.     export PYTHONPATH
  100.  
  101.     # We'll save and restore the Caps_Lock as a modifier just in case
  102.     # the user is using the Caps_Lock as the Orca modifier key.
  103.     #
  104.     if [ "x$DISPLAY" != "x" ]
  105.     then
  106.         CAPSLOCKSETTING=`xmodmap | grep Caps_Lock | cut -f1`
  107.     fi
  108.     /usr/bin/python -c "import orca.orca; orca.orca.main()" "$ARGS"
  109.     if [ "x$CAPSLOCKSETTING" != "x" ]
  110.     then
  111.         xmodmap -e "add $CAPSLOCKSETTING = Caps_Lock"
  112.     fi
  113. }
  114.  
  115. # Runs a watchdog process in the background.  It merely attempts to
  116. # get to Orca via some other means than the AT-SPI.  Here we use
  117. # Orca's http server at port 20433.  If it doesn't respond, then
  118. # we assume Orca is dead.
  119. #
  120. watchdog()
  121. {
  122.     (
  123.         sleep 30 # Give orca a chance to start.
  124.         while [ "$WATCHDOG" -gt 0 ]
  125.         do
  126.             sleep 5
  127.             USERID=`id | cut -f2 -d= | cut -f1 -d\(`
  128.             PID=`ps -eo pid,ruid,args | grep $USERID | egrep "orca[.]orca" | grep -v grep | awk '{ print $1 }'`
  129.  
  130.             if [ "x$PID" = "x" ]
  131.             then
  132.                 exit
  133.             else
  134.                 $WGETCMD -q -t 0 -O /dev/null -w 2 "http://localhost:20433" || {
  135.                     echo Orca watchdog detected something bad.  Cleaning up.
  136.                     cleanup
  137.                 }
  138.             fi
  139.         done
  140.     ) &
  141. }
  142.  
  143. kill_watchdog()
  144. {
  145.     if [ "x$watchdog_pid" != x0 ]
  146.     then
  147.         kill -9 $watchdog_pid > /dev/null 2>&1
  148.     fi
  149. }
  150.  
  151. kill_orca()
  152. {
  153.     kill_watchdog
  154.     cleanup
  155.     exit
  156. }
  157.  
  158. hup_orca()
  159. {
  160.     cleanup
  161. }
  162.  
  163. main()
  164. {
  165.     if [ "$WATCHDOG" -gt 0 ]
  166.     then
  167.         watchdog
  168.         watchdog_pid=$!
  169.     fi
  170.     while [ "$RUN" -gt 0 ]
  171.     do
  172.         runOrca &
  173.         orca_pid=$!
  174.         wait $orca_pid
  175.  
  176.         RUN=$?  # quit on a normal exit status from Orca
  177.  
  178.         # We will stop re-running Orca on SEGV's (139 = SEGV + 128).
  179.         # The reason for this is that there are cases where Python
  180.         # will SEGV when Orca attempts to exit normally.  This happens
  181.         # because of something going on in pyorbit.  This should be
  182.         # fixed in pyorbit 2.14.1, but not everyone has that.
  183.         # So...we'll check for it.
  184.         #
  185.         if [ "$RUN" -eq 139 ]
  186.         then
  187.             RUN=0
  188.         fi
  189.  
  190.         # We will also stop re-running Orca on KILL's (137 = KILL + 128).
  191.         # The reason for this is that if someone has done a "kill -KILL"
  192.         # on the Python process, it was probably on purpose and they want
  193.         # everything to die.
  194.         #
  195.         if [ "$RUN" -eq 137 ]
  196.         then
  197.             RUN=0
  198.         fi
  199.     done
  200.     kill_watchdog
  201. }
  202.  
  203. trap kill_orca QUIT TERM INT ABRT
  204. trap hup_orca HUP
  205.  
  206. # Orca will fall into a text-based question and answer session if the
  207. # user has not configured orca and/or accessibility yet.  We will
  208. # force that to happen in the foreground (i.e., RUNONCE=true).  In
  209. # addition, if the user passes any command line arguments to orca, we
  210. # will run it in the foreground as well to avoid a situation where
  211. # orca dumps itself into the text-based setup utility.
  212. #
  213. # We make a special exception for gdm, which is used to handle the
  214. # accessible login.  If we're running as gdm, we assume everything is
  215. # all set and we don't need to muck around.
  216. #
  217. if [ "x$LOGNAME" != "xgdm" ]
  218. then
  219.     ACCESSIBILITY_ENABLED=`gconftool-2 --get /desktop/gnome/interface/accessibility`
  220.     if [ "x$ACCESSIBILITY_ENABLED" != "xtrue" ]
  221.     then
  222.         # Because we will be running Orca in text-setup mode, we want to
  223.         # make sure it is run in a terminal window.  If we're already in
  224.         # a terminal, this is great.  If not, we spawn a gnome-terminal
  225.         # and run orca in it.
  226.         #
  227.         tty -s && IN_TTY="true" || IN_TTY="false"
  228.         if [ "x$IN_TTY" = "xtrue" ]
  229.         then
  230.             RUNONCE="true"
  231.         else
  232.             exec gnome-terminal -x $0 $ARGS
  233.         fi
  234.     fi
  235. fi
  236.  
  237. if [ "x$RUNONCE" = "xfalse" -a "x$ARGS" = "x" ]
  238. then
  239.     main
  240. else
  241.     if [ `grep -c "\-q" <<< $ARGS` -gt 0 ]
  242.     then
  243.         cleanup
  244.     else
  245.         # If the user passed in a flag that results in orca only
  246.         # outputting data to the console, don't kill any other orca
  247.         # process.  We do this by looking for flags that *should*
  248.         # result in a cleanup (i.e., every legal command except
  249.         # -?, --help, -v, and --version).  This way, if the user
  250.         # erroneously types an illegal command line argument, the
  251.         # help text is emitted and the other orca is not killed.
  252.         #
  253.         if [ `egrep -c "\-s|\-g|\-t|\-n|\-u|\-e|\-d" <<< $ARGS` -eq 0 ]
  254.         then
  255.             runOrca "NO_CLEANUP"
  256.         else
  257.             runOrca
  258.         fi
  259.     fi
  260. fi
  261.